<?php
namespace app\index\controller;
use app\index\model\reimbursementTable;
use app\index\model\UserTable;
class Reimbursement extends Common
{
	public function reimbursement(){
		$id = request()->param('id');
		//halt($id);
		$info = UserTable::get($id);
		if($info['username'] !== cookie('username')){
			return $this->redirect('Index/index');
		}
			$this->assign('info',$info);
		return view();
	}
	public function reimbursementData(){
		$page = request()->param('page');
		$limit = request()->param('limit');
		$start = ($page-1)*$limit;
		$djbh = request()->param('value');
		if($djbh == ''){
			$userinfo = reimbursementTable::where('username',cookie('username'))->limit($start,$limit)->order('create_time','desc')->select();
			$count = count(reimbursementTable::where('username',cookie('username'))->select());
		}else{
			$userinfo = reimbursementTable::where('username',cookie('username'))->withSearch(['document_number'],['document_number'=>$djbh])->limit($start,$limit)->order('create_time','desc')->select();
			$count = count(reimbursementTable::where('username',cookie('username'))->withSearch(['document_number'],['document_number'=>$djbh])->select());
		}
		
		$msg = [
			'code'=>0,
			'msg'=>'查询成功',
			'data'=>$userinfo,
			'count'=>$count
		];
		echo json_encode($msg);die;
	}
	public function reimbursementadd(){	
		$id = cookie('id');
		$info = UserTable::get($id);
		if($info['username'] !== cookie('username')){
			return $this->redirect('Index/index');
		}
		$this->assign('info',$info);
		return view();
	}
	public function reimbursementuploadfie(){
		$name = Cookie('username');
		$file = request()->file('file');
		// 移动到框架应用根目录/uploads/ 目录下
		$info = $file->validate(['ext'=>'png,jpg'])->move( './static/uploads/'.$name.'/','');
		if($info){
			return ['code'=>0,'msg'=>$info->getSaveName()];
		}else{
		    // 上传失败获取错误信息
			return ['code'=>1,'msg'=>$file->getError()];
		}
	}
	public function reimbursementadddata(){
		$data = request()->param();
		$data['file'] = basename($data['file']);
		/* halt($data); */
		return (new reimbursementTable())->reimbursementadddata($data);
	}
	public function reimbursementedit(){
		/* $id = cookie('id');
		$info = UserTable::get($id);
		if($info['username'] !== cookie('username')){
			return $this->redirect('Index/index');
		}
		$this->assign('info',$info); */
		
		$id = request()->param('id');
		/* halt($id); */
		$res = reimbursementTable::where('id',$id)->find();
		/* $data=array($res); */
		$this->assign('res',$res);
		return view();
	}
	public function reimbursementeditdata(){
		$data = request()->param();
		$data['file'] = basename($data['file']);
		/* halt($data); */
		return (new reimbursementTable())->reimbursementeditdata($data);
	}
	public function reimbursementdel(){
		$id = request()->param('id');
		return (new reimbursementTable())->reimbursementdel($id);
	}
}